package com.mydemo.temp;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.event.SyncReadListener;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.*;
import java.util.stream.Collectors;

public class Temp {

    static String nameMap = "{\"100000066952\":\"13311759187\",\"100000210431\":\"13601024166\",\"100001440040\":\"15652333307\",\"100001781521\":\"13302019518\",\"100002890875\":\"13505335808\",\"100002893588\":\"18851685666\",\"100003121241\":\"18982401689\",\"100003162676\":\"11393298@qq.com\",\"100003167046\":\"18678657321\",\"100003169762\":\"13333863600\",\"100003227482\":\"13523057615\",\"100003234124\":\"13953757234\",\"100003275636\":\"15890193207\",\"100003278391\":\"15713667520\",\"100003300493\":\"13783588315\",\"200003018629\":\"1020059746@qq.com\",\"200003067629\":\"hongyajieneng@126.com\",\"200003077789\":\"luohuayue@whkfny.cn\",\"200003120672\":\"zhangke1@haier.com\",\"200003121771\":\"306436960@qq.com\",\"200003133568\":\"604064177@qq.com\",\"200003160123\":\"372824069@qq.com\",\"200003161152\":\"hbxkms@126.com\",\"200003232335\":\"17803860999@139.com\",\"200003258493\":\"sfglk@wfmc.edu.cn\",\"200003265980\":\"shunxianshiye@163.com\",\"200003267756\":\"236882164@qq.com\",\"200003302957\":\"gzmjgcjs@163.com\"}";

    public static void main(String[] args) {
        List<Export> datas = EasyExcel.read("export-er.xlsx").head(Export.class).sheet().doReadSync();
        List<TradeCompareBillDetail> tradeCompareBillDetails = translateDetailList(datas, createInfoMap("200003120672,北京海享汇科技有限公司;200003267999,北京海享汇科技有限公司"), createInfoMap("FYMC2021004,快捷通充值;FYMC2021002,海享汇服务收入-YF;FYMC2021003,账户扣费;FYMC2021001,海享汇服务收入-SR"));
        EasyExcel.write("export-result.xlsx", TradeCompareBillDetail.class).sheet("模板").doWrite(tradeCompareBillDetails);
    }

    public static List<TradeCompareBillDetail> translateDetailList(List<Export> list, Map<String, String> compareBillPartnerMap, Map<String, String> itemMap) {
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        Set<String> names = new HashSet<>();
        List<TradeCompareBillDetail> result = new ArrayList<>();
        JSONObject map = JSONObject.parseObject(nameMap);
        for (Export detail : list) {
            TradeCompareBillDetail billDetail = new TradeCompareBillDetail();
            BeanUtils.copyProperties(detail, billDetail);
            billDetail.setDetailId(detail.getId());
            // 对账不平的时候，部分字段为空，需要额外判断
            if (detail.getKjtAmount() != null) {
                billDetail.setKjtName("快捷通");
                billDetail.setKjtAmount(detail.getKjtAmount().toString());
                billDetail.setKjtTradeStatus(EnumJobStatus.getMsgByCode(detail.getKjtTradeStatus()));
                billDetail.setTradeType(TradeTypeEnum.getMsgByCode(detail.getTradeType()));
            }
            billDetail.setOtherName(compareBillPartnerMap.get(detail.getMemberId()));
            billDetail.setExpenseItemCode(detail.getExpenseItemCode());
            billDetail.setExpenseItemName(itemMap.get(detail.getExpenseItemCode()));
            if (detail.getOtherAmount() != null) {
                billDetail.setOtherAmount(detail.getOtherAmount().toString());
                billDetail.setOtherTradeStatus(MerchantTradeOrderStatusEnum.parse(detail.getOtherTradeStatus()).getMessage());
            }
            if (detail.getCompareStatus() != null) {
                billDetail.setCompareStatus(MerchantTradeCompareStatusEnum.parse(detail.getCompareStatus()).getMessage());
            }
            if (detail.getHandleStatus() != null) {
                billDetail.setHandleStatus(MerchantTradeCompareHandleStatusEnum.parse(detail.getHandleStatus()).getMessage());
            }
            billDetail.setBusinessDate(detail.getGmtTxn().substring(0, 19));
            if (StringUtils.isEmpty(detail.getGmtModified1())) {
                billDetail.setBusinessDate(detail.getGmtModified1().substring(0, 19));
            }
            if (!StringUtils.isEmpty(detail.getSplitParty())) {
                List<SplitParty> splitPartyList = JSON.parseArray(detail.getSplitParty(), SplitParty.class);
                if (!CollectionUtils.isEmpty(splitPartyList)) {
                    String splitAmount = "";
                    String splitIdentity = "";
                    for (SplitParty split : splitPartyList) {
                        splitAmount = splitAmount + split.getAmount() + "|";
                        splitIdentity = splitIdentity + map.getString(split.getPartyId()) + "|";
                        names.add(split.getPartyId());
                    }
                    billDetail.setSplitAmount(splitAmount.substring(0, splitAmount.length() - 1));
                    billDetail.setSplitIdentity(splitIdentity.substring(0, splitIdentity.length() - 1));
                }
            }
            result.add(billDetail);
        }
        System.out.println(names);
        return result;
    }

    private static Map<String, String> createInfoMap(String info) {
        String[] partners = info.split(";");
        List<String> singlePartnerList = Arrays.asList(partners);
        Map<String, String> map = new HashMap<>(8);
        for (String singlePartner : singlePartnerList) {
            String[] partner = singlePartner.split(",");
            map.put(partner[0], partner[1]);
        }
        return map;
    }
}
